package com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.ImageBean;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ImageBean.DataBeanX.DataBean> dataBeans = new ArrayList<>();

    public ImageAdapter(Context context) {
        this.context = context;
    }

    public void addList(List<ImageBean.DataBeanX.DataBean> dataBeans) {
        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.image_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        ImageBean.DataBeanX.DataBean dataBean = dataBeans.get(position);
        Glide.with(context).load(dataBean.getAuthor().getAvatar()).into(viewHolder.iv_image_image);
        viewHolder.tv_text_image.setText(dataBean.getAuthor().getName());
        Glide.with(context).load(dataBean.getCover()).into(viewHolder.image_image);
        //title
        Object feeds_text = dataBean.getFeeds_text();
        if(feeds_text==null){
            viewHolder.tv_title_image.setText("");
        }else {
            viewHolder.tv_title_image.setText(feeds_text+"");
        }
        //音乐时刻
        Object activityText = dataBean.getActivityText();

        if(activityText==null){
            viewHolder.tv_search_image.setVisibility(View.GONE);
        }else {
            viewHolder.tv_search_image.setText(activityText+"");
        }
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public static
    class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public ImageView iv_image_image;
        public TextView tv_text_image;
        public TextView tv_title_image;
        public ImageView image_image;
        public TextView tv_search_image;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.iv_image_image = (ImageView) rootView.findViewById(R.id.iv_image_image);
            this.tv_text_image = (TextView) rootView.findViewById(R.id.tv_text_image);
            this.tv_title_image = (TextView) rootView.findViewById(R.id.tv_title_image);
            this.image_image = (ImageView) rootView.findViewById(R.id.image_image);
            this.tv_search_image = (TextView) rootView.findViewById(R.id.tv_search_image);
        }

    }
}
